/*
 * ============================================================================
 * 数组 (Array) - C#实现
 * ============================================================================
 * C#提供: Array(固定大小) 和 List<T>(动态数组)
 * 特色: 泛型、LINQ、自动内存管理
 */

using System;
using System.Collections.Generic;
using System.Linq;

namespace DataStructures
{
    public class DynamicArray<T>
    {
        private T[] _data;
        private int _size;
        private int _capacity;

        public DynamicArray(int capacity = 10)
        {
            _capacity = capacity;
            _size = 0;
            _data = new T[capacity];
        }

        public int Count => _size;
        
        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= _size)
                    throw new IndexOutOfRangeException();
                return _data[index];
            }
            set
            {
                if (index < 0 || index >= _size)
                    throw new IndexOutOfRangeException();
                _data[index] = value;
            }
        }

        public void Add(T item)
        {
            if (_size >= _capacity)
                Resize(_capacity * 2);
            _data[_size++] = item;
        }

        public void Insert(int index, T item)
        {
            if (index < 0 || index > _size)
                throw new IndexOutOfRangeException();
            
            if (_size >= _capacity)
                Resize(_capacity * 2);
            
            Array.Copy(_data, index, _data, index + 1, _size - index);
            _data[index] = item;
            _size++;
        }

        public void RemoveAt(int index)
        {
            if (index < 0 || index >= _size)
                throw new IndexOutOfRangeException();
            
            Array.Copy(_data, index + 1, _data, index, _size - index - 1);
            _data[--_size] = default(T);
        }

        private void Resize(int newCapacity)
        {
            T[] newData = new T[newCapacity];
            Array.Copy(_data, newData, _size);
            _data = newData;
            _capacity = newCapacity;
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _data.Take(_size))}]";
        }
    }

    // 应用场景演示
    public class ArrayExamples
    {
        // 场景1: 查找表
        public static void LookupTable()
        {
            Console.WriteLine("\n=== 应用场景1: 查找表 ===");
            int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            int month = 7;
            Console.WriteLine($"第{month + 1}个月有{daysInMonth[month]}天");
            Console.WriteLine("优势: O(1)随机访问");
        }

        // 场景2: LINQ查询
        public static void LinqExample()
        {
            Console.WriteLine("\n=== 应用场景2: LINQ查询 ===");
            var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            
            var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
            var sum = numbers.Sum();
            
            Console.WriteLine($"偶数: {string.Join(", ", evenNumbers)}");
            Console.WriteLine($"总和: {sum}");
            Console.WriteLine("优势: C#的LINQ提供强大的查询能力");
        }

        // 场景3: 泛型数组
        public static void GenericArray()
        {
            Console.WriteLine("\n=== 应用场景3: 泛型数组 ===");
            var intArray = new DynamicArray<int>();
            intArray.Add(10);
            intArray.Add(20);
            intArray.Add(30);
            Console.WriteLine($"整数数组: {intArray}");
            
            var stringArray = new DynamicArray<string>();
            stringArray.Add("Hello");
            stringArray.Add("World");
            Console.WriteLine($"字符串数组: {stringArray}");
            Console.WriteLine("优势: 类型安全，编译时检查");
        }
    }

    // 主程序
    public class ArrayProgram
    {
        public static void Main()
        {
            Console.WriteLine("╔════════════════════════════════════════╗");
            Console.WriteLine("║   数组 (Array) - C#实现                ║");
            Console.WriteLine("╚════════════════════════════════════════╝");

            var arr = new DynamicArray<int>();
            arr.Add(10);
            arr.Add(20);
            arr.Add(30);
            Console.WriteLine($"\n数组: {arr}");
            Console.WriteLine($"大小: {arr.Count}");

            ArrayExamples.LookupTable();
            ArrayExamples.LinqExample();
            ArrayExamples.GenericArray();

            Console.WriteLine("\n✅ 优势: 泛型、LINQ、类型安全");
            Console.WriteLine("⚠️ 局限: 插入删除O(n)");
            Console.WriteLine("💡 应用: 数据存储、LINQ查询、集合操作");
        }
    }
}
